package com.ncloud.chnl.business;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.chnl.common.Constant;
import com.ncloud.chnl.entity.Chnl;
import com.ncloud.chnl.entity.ChnlDeposit;
import com.ncloud.chnl.entity.ChnlDepositLog;
import com.ncloud.chnl.mapper.ChnlDepositLogMapper;
import com.ncloud.chnl.mapper.ChnlDepositMapper;
import com.ncloud.chnl.mapper.ChnlMapper;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.param.service.SequenceService;

@Service
public class ChnlDepositBusiness {
	@Autowired
	private ChnlDepositLogMapper logMapper;
	@Autowired
	private ChnlDepositMapper depositMapper;
	@Autowired
	private ChnlMapper chnlMapper;
	@Reference
	private SequenceService sequenceService;
	
	public JSONObject insertChnlDeposit(Long chnlId, Long userId) {
		ChnlDeposit deposit = new ChnlDeposit();
		deposit.setChnlId(chnlId);
		deposit.setCreateUser(userId);
		deposit.setUpdateUser(userId);
		deposit.setAvialableValue(0L);
		deposit.setDeposit(0L);
		deposit.setCreditValue(0L);
		deposit.setState(Constant.CHNL_DEPOSIT_STATE_NORMAL);
		deposit.setCreateTime(new Date());
		deposit.setUpdateTime(new Date());
		depositMapper.insertChnlDeposit(deposit);
		return new JSONObject();
	}

	@Transactional
	public JSONObject adjustChnlDeposit(JSONObject json) {
		String actionCode = json.getString("actionCode");
		Long chnlId = json.getLong("chnlId");
		Long value = getMultiMoney(json.get("value"),100);
		ChnlDeposit deposit = depositMapper.getChnlDeposit(chnlId);
		ChnlDepositLog depositLog = new ChnlDepositLog();
		depositLog.setOldValue(deposit);
		if(Constant.DepositAction.adjustCredit.getCode().equals(actionCode)){
			deposit.setAvialableValue(deposit.getAvialableValue()-deposit.getCreditValue()+value);
			deposit.setCreditValue(value);
			depositLog.setRecvType(Constant.DepositAction.adjustCredit.getValue());
		}else if(Constant.DepositAction.payDeposit.getCode().equals(actionCode)){
			deposit.setDeposit(deposit.getDeposit()+value);
			deposit.setAvialableValue(deposit.getAvialableValue()+value);
			depositLog.setRecvType(Constant.DepositAction.payDeposit.getValue());
		}
		deposit.setUpdateTime(new Date());
		deposit.setUpdateUser(json.getLong("userID"));
		depositLog.setNewValue(deposit);
		depositMapper.updateChnlDeposit(deposit);
		depositLog.setRecvValue(value);
		depositLog.setLogId(sequenceService.getSequence("DEPOSIT_LOG_ID_SEQ"));
		logMapper.insertChnlDepositLog(depositLog);
		//如果存在未销账账单并且保证金账户余额大于0，发起销账流程
		if(exsitsUnWriteOffBill(chnlId) && deposit.getDeposit()>0){
			// TODO 销账
		}
		return new JSONObject();
	}
	
	private boolean exsitsUnWriteOffBill(Long chnlId) {
		// TODO Auto-generated method stub
		return false;
	}

	/*对属性值是金额的code进行处理,*/
    public static Long getMultiMoney(Object money,int precision){
        BigDecimal bigDecimal = new BigDecimal(String.valueOf(money));
        return bigDecimal.multiply(BigDecimal.valueOf(precision)).longValue();
    }

    @Transactional
	public JSONObject preAbateChnlDeposit(JSONObject json) {
		Long userId = json.getLong("userID");
		Chnl chnl = chnlMapper.getChnlByUserId(userId);
		ChnlDeposit deposit = depositMapper.getChnlDeposit(chnl.getChnlId());
		ChnlDepositLog depositLog = new ChnlDepositLog();
		depositLog.setRecvType(Constant.DepositAction.preAbate.getValue());
		depositLog.setOrderId(json.getLong("orderId"));
		depositLog.setOldValue(deposit);
		// 判断可用额度是否足够预扣
		Long value = getMultiMoney(json.get("value"), 100);
		if(deposit.getAvialableValue()>=value){
			deposit.setAvialableValue(deposit.getAvialableValue()-value);
			deposit.setUpdateTime(new Date());
			deposit.setUpdateUser(userId);
			depositLog.setNewValue(deposit);
			depositLog.setRecvValue(value);
		}else{
			throw new UserException(ResultEnum.DEPOSIT_NO_MORE_VALUE);
		}
		
		depositMapper.updateChnlDeposit(deposit);
		depositLog.setLogId(sequenceService.getSequence("DEPOSIT_LOG_ID_SEQ"));
		logMapper.insertChnlDepositLog(depositLog);
		return new JSONObject();
	}
	
}
